﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ObjectPool : MonoBehaviour
{
    public GameObject[] objectPrefabs;
    private List<GameObject> pooledObjs = new List<GameObject>();
    public Transform pos3;
    public static int boosCount = 1;
    //private void Start()
    //{
    //    Debug.Log(objectPrefabs[0].name);
    //}
    public GameObject Generate(string type, bool isActive, GameObject boosgameobject)
    {
        for (int i = 0; i < objectPrefabs.Length; i++)
        {
            if (objectPrefabs[i].name.Equals(type))
            {
                GameObject newObject = Instantiate(objectPrefabs[i]);
                newObject.SetActive(isActive);

                if (objectPrefabs[i].name.Contains("HealthBar"))
                    newObject.transform.SetParent(pos3);
                else
                {
                    newObject.transform.SetParent(pos3);
                    newObject.GetComponent<Image>().color = new Color(1, 1, 1, 0);
                    newObject.transform.GetChild(1).gameObject.SetActive(false);
                    newObject.transform.GetChild(2).gameObject.SetActive(false);
                }

                //控制掉落范围
                newObject.transform.position = new Vector3(boosgameobject.transform.position.x + Random.Range(-0.5f, 0.5f), boosgameobject.transform.position.y + Random.Range(-0.5f, 0.5f), 0);
                newObject.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
                pooledObjs.Add(newObject);
                newObject.name = type;
                return newObject;
            }
        }
        return null;
    }

    public GameObject GetObject(string type, GameObject boosgameobject)
    {
        //Debug.Log("type------" + type);
        //Debug.Log(pooledObjs.Count);
        return Generate(type, true, boosgameobject);
    }
}
